<?php
include('../conn.php');
include('../func/check_param.php');
error_reporting(0);
/*
从购物车获取数据添加订单
*/


// 必传参数 
$param = array('userid','total');
// 必传参数 end

$addressid = $_REQUEST['addressid'];
$goodsid = $_REQUEST['goodsid'];
$addnum = $_REQUEST['addnum'];
$carid = $_REQUEST['carid'];

check_param($param,function($p){
	global $addressid;
	global $goodsid;
	global $addnum;
	global $carid;
	

	if ($carid != '') {
		$query = mysql_query("SELECT * FROM car WHERE carid IN (".$carid.") LIMIT 1 ");
		$car = mysql_fetch_assoc($query);
		$shopid = $car[shopid];
	}else{
		$query = mysql_query("select * from goods where goodsid = ".$goodsid);
		$goods = mysql_fetch_assoc($query);
		$shopid = $goods[shopid];
	}
	

	// 查询默认地址 
	$query = mysql_query("select * from address where  userid ='".$p['userid']."' and state = 1");
	$result = mysql_fetch_assoc($query);
	$address_detail = $result[province].",".$result[city].",".$result[quyu].",".$result[address];
	$address = array();
	$address['dizhiid'] = $result['id'];
	$address['name'] = $result['name'];
	$address['dizhi'] = $address_detail;
	$address['dianhua'] = $result['tel'];

	// 查询默认地址 end


	// 查找送货地址
	if ($addressid != '') {
		$query = mysql_query("select * from address where id=".$addressid);
	}else{
		// 默认地址
		$query = mysql_query("select * from address where userid=".$p['userid']." and state=1");
		// 默认地址 end
	}
	$myaddress = mysql_fetch_assoc($query);
	// 查找送货地址 end

	if ($carid != '') {
		$query_muid = mysql_query("select goodsid,muid,zixilie,shopid from goods where goodsid in (select goodsid from car where carid in (".$carid."))");
	}else{
		$query_muid = mysql_query("select goodsid,muid,zixilie,shopid from goods where goodsid = ".$goodsid."");
	}
	// 遍历商品 end

	// 计算运费
	$fare_total = 0;
	$allFreight = 0;
	while ($row_muid = mysql_fetch_assoc($query_muid)) {
		$sql = mysql_query("select type from zifenlei where id=".$row_muid[zixilie]);
		$type = mysql_fetch_assoc($sql);
		$type = $type['type'];
		if ($type == 1) { // 快购
			$query = mysql_query("select distribution_fee from shop where shopid=".$row_muid['shopid']);
			$distribution_fee = mysql_fetch_assoc($query);
			$distribution_fee = $distribution_fee['distribution_fee'];
			$fare_total += $distribution_fee;
		}else{ // 海淘
			// 运费条件
			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[quyu]."%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result0 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[city]."%'  AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result1 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result2 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%0%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = ".$shopid." and isPay = 0)");
			$result3 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[quyu]."%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result4 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[city]."%'  AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid = ".$row_muid['muid']." and muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result5 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result6 = mysql_fetch_assoc($sql);

			$sql = mysql_query("select * from fare where `area` like '%0%' and muid = ".$row_muid['muid']." and  muid in (select id from fareml where shopid = 0 and isPay = 0)");
			$result7 = mysql_fetch_assoc($sql);
			if ($result0) {
				$where_area = "AND `area` like '%".$myaddress[quyu]."%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result1) {
				$where_area = "AND `area` like '%".$myaddress[city]."%' AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result2) {
				$where_area = "AND `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result3){
				$where_area = "AND `area` like '%0%' and muid in (select id from fareml where shopid = ".$shopid.")";
			}elseif ($result4) {
				$where_area = "AND `area` like '%".$myaddress[quyu]."%' and muid in (select id from fareml where shopid = 0)";
			}elseif ($result5) {
				$where_area = "AND `area` like '%".$myaddress[city]."%' AND `area` NOT LIKE '%".$myaddress[city]."-%' and muid in (select id from fareml where shopid = 0)";
			}elseif ($result6) {
				$where_area = "AND `area` like '%".$myaddress[province]."%' AND `area` NOT LIKE '%".$myaddress[province]."-%' and muid in (select id from fareml where shopid = 0)";
			}else{
				$where_area = "AND `area` like '%0%' and muid in (select id from fareml where shopid = 0)";
			}


			// 运费条件 end


			// 统计商品数量 
			if ($carid != '') {
				$query = mysql_query("select sum(number) as sum from car where goodsid = ".$row_muid['goodsid']." and carid in (".$carid.") and state=0");
				$number = mysql_fetch_assoc($query);
				$addnum = $number['sum'];
			}else{
				$addnum = $addnum;
			}
			// 统计商品数量 end



			// 计算运费 

			$sql = mysql_query("SELECT CASE WHEN ".$addnum.">begincount THEN addfare*(".$addnum."-begincount)+fare ELSE fare END AS fare FROM fare WHERE muid = ".$row_muid['muid']."  ".$where_area."");
			$fare = mysql_fetch_assoc($sql);
			$distribution_fee = $fare['fare'];
			if (!$distribution_fee) {
				$distribution_fee = 0;
			}
			$fare_total += $distribution_fee; 
			// 计算运费 end

			// // 计算运费险
			$query = mysql_query("select * from freightrisk");
			$freightRisk = 0;
			while ($row = mysql_fetch_assoc($query)) {
				if ($addnum >= $row[goodsNum]) {
					$freightRisk = $row[freightRisk];
				}
			}
			$allFreight += $freightRisk;
			// // 计算运费险 end
		}
	}


	// 优惠券列表
	$query = mysql_query("select * from coupon left join mycoupon on coupon.couponid=mycoupon.couponid  where (coupon.shopid=".$shopid." || coupon.shopid=0) and mycoupon.userid='".$p['userid']."' and mycoupon.is_use=0 ");

	$coupon = array();
	$i = 0;
	while ($row=mysql_fetch_assoc($query)) {
		if ($p['total']>=$row['top_price']) {
			$coupon[$i]=$row;
			$i++;
		}
	}
	// 优惠券列表 end

	// 商家信息 
	$query = mysql_query("select freight_ins,name,shopid from shop where shopid=".$shopid);
	$result = mysql_fetch_assoc($query);

	$deliver_type = json_decode($deliver_type);
	
	$shop['shopid'] = $shopid;
	$shop['name'] = $result['name'];
	$shop['freight_ins'] = $result['freight_ins'];
	// 商家信息 end

	$arr = array('code' => 200, 'msg' => '查询成功', 'address' => $address, 'distribution_fee' => $fare_total, 'coupon' => $coupon,"shop"=>$shop, 'freightRisk' => $allFreight, 'services' => $services);
	die(json_encode($arr));
});

